/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.builder;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

/**
 * @author Clinton Begin
 */
/**
 * 构建器的基类，建造者模式
 *
 */
public abstract class BaseBuilder {
	/**
	 * 配置
	 */
	protected final Configuration configuration;
	/**
	 * 类型别名注册
	 */
	protected final TypeAliasRegistry typeAliasRegistry;
	/**
	 * 类型处理器注册
	 */
	protected final TypeHandlerRegistry typeHandlerRegistry;

	/**
	 * 构造器
	 * 
	 * @param configuration
	 *            配置
	 */
	public BaseBuilder(Configuration configuration) {
		this.configuration = configuration;
		this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
		this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
	}

	/**
	 * 得到配置
	 * 
	 * @return
	 */
	public Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * 解析表达式
	 * 
	 * @param regex
	 *            正则表达式
	 * @param defaultValue
	 *            默认的值
	 * @return
	 */
	protected Pattern parseExpression(String regex, String defaultValue) {
		return Pattern.compile(regex == null ? defaultValue : regex);
	}

	/**
	 * 转换成boolean类型
	 * 
	 * @param value
	 *            值
	 * @param defaultValue
	 *            默认的值
	 * @return
	 */
	protected Boolean booleanValueOf(String value, Boolean defaultValue) {
		return value == null ? defaultValue : Boolean.valueOf(value);
	}

	/**
	 * 转换成Integer类型
	 * 
	 * @param value
	 *            值
	 * @param defaultValue
	 *            默认的值
	 * @return
	 */
	protected Integer integerValueOf(String value, Integer defaultValue) {
		return value == null ? defaultValue : Integer.valueOf(value);
	}

	/**
	 * 把以逗号分割的一个字符串重新包装，返回一个Set
	 * 
	 * @param valuec
	 *            值
	 * @param defaultValue
	 *            默认的值
	 * @return
	 */
	protected Set<String> stringSetValueOf(String value, String defaultValue) {
		value = (value == null ? defaultValue : value);
		return new HashSet<String>(Arrays.asList(value.split(",")));
	}

	/**
	 * 解析JdbcType
	 * 
	 * @param alias 别名
	 * @return
	 */
	protected JdbcType resolveJdbcType(String alias) {
		if (alias == null) {
			return null;
		}
		try {
			return JdbcType.valueOf(alias);
		} catch (IllegalArgumentException e) {
			throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
		}
	}

	/**
	 * 解析ResultSetType
	 * 
	 * @param alias 别名
	 * @return
	 */
	protected ResultSetType resolveResultSetType(String alias) {
		if (alias == null) {
			return null;
		}
		try {
			return ResultSetType.valueOf(alias);
		} catch (IllegalArgumentException e) {
			throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
		}
	}

	/**
	 * 解析ParameterMode(SP的IN/OUT/INOUT)
	 * 
	 * @param alias 别名
	 * @return
	 */
	protected ParameterMode resolveParameterMode(String alias) {
		if (alias == null) {
			return null;
		}
		try {
			return ParameterMode.valueOf(alias);
		} catch (IllegalArgumentException e) {
			throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
		}
	}

	/**
	 * 根据别名解析Class，然后创建实例
	 * 
	 * @param alias 别名
	 * @return
	 */
	protected Object createInstance(String alias) {
		Class<?> clazz = resolveClass(alias);
		if (clazz == null) {
			return null;
		}
		try {
			return resolveClass(alias).newInstance();
		} catch (Exception e) {
			throw new BuilderException("Error creating instance. Cause: " + e, e);
		}
	}

	/**
	 * 根据别名解析Class,其实是去查看 类型别名注册/事务管理器别名
	 * 
	 * @param alias 别名
	 * @return
	 */
	protected Class<?> resolveClass(String alias) {
		if (alias == null) {
			return null;
		}
		try {
			return resolveAlias(alias);
		} catch (Exception e) {
			throw new BuilderException("Error resolving class. Cause: " + e, e);
		}
	}

	/**
	 * 解析类型处理器
	 * 
	 * @param javaType java类型
	 * @param typeHandlerAlias 类型处理器别名
	 * @return
	 */
	protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
		if (typeHandlerAlias == null) {
			return null;
		}
		// 先取得别名所属的Class
		Class<?> type = resolveClass(typeHandlerAlias);
		// 如果不是TypeHandler的子类,报错
		if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
			throw new BuilderException("Type " + type.getName()
					+ " is not a valid TypeHandler because it does not implement TypeHandler interface");
		}
		@SuppressWarnings("unchecked") // already verified it is a TypeHandler
		Class<? extends TypeHandler<?>> typeHandlerType = (Class<? extends TypeHandler<?>>) type;
		// 再去调用另一个重载的方法
		return resolveTypeHandler(javaType, typeHandlerType);
	}
	/**
	 * 解析类型处理器
	 * @param javaType java类型
	 * @param typeHandlerType 类型处理器
	 * @return
	 */
	protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
		if (typeHandlerType == null) {
			return null;
		}
		// javaType ignored for injected handlers see issue #746 for full detail
		// 去typeHandlerRegistry查询对应的TypeHandler
		TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
		if (handler == null) {
			// not in registry, create a new one
			// 如果没有在Registry找到，调用typeHandlerRegistry.getInstance来new一个TypeHandler返回
			handler = typeHandlerRegistry.getInstance(javaType, typeHandlerType);
		}
		return handler;
	}

	/**
	 * 解析别名
	 * @param alias 别名
	 * @return
	 */
	protected Class<?> resolveAlias(String alias) {
		return typeAliasRegistry.resolveAlias(alias);
	}
}
